package me.pplay.playtalk.activity.friends;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.adapter.CommonAdapter;
import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.BuddyInfoUtil;
import me.pplay.playtalk.util.LogManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class SearchResultActivity extends BaseActivity {
	private List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
	private ListView list;
	private TpChat tpChat;
	private CommonControlID holderId = new CommonControlID();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_searchresult);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat = appState.getTpChat();
		mData = getData();
		
		holderId.itemLayout = R.layout.layout_discoverlist;
		holderId.leftImgId = R.id.discover_img;
		holderId.rightImgId = R.id.discover_add;
		holderId.leftStrId = R.id.discover_title;
		holderId.leftStr1Id = R.id.discover_topic;
		CommonAdapter adapter = new CommonAdapter(SearchResultActivity.this, mData, holderId, new CommonAdapterOnClickCallBack() {

			@Override
			public void OnClickCallback(int pos, int ViewId, View v) {
				// TODO Auto-generated method stub
				LogManager.LogShow("SearchResultActivity", "OnClickCallback position " + pos);
				if(holderId.rightImgId == ViewId)
				{
					LinkedList<TPBuddyinfo> flist = tpChat.tp_chat_search_friends_data();
					if(flist!=null){
						TPBuddyinfo buddyinfo = flist.get(pos);
						if(buddyinfo!=null){
							int skyid = buddyinfo.getSkyid();
							buddyinfo = tpChat.tp_chat_getBuddyinfo(skyid);
							if(buddyinfo == null) 
								tpChat.tp_chat_add_buddy(BuddyInfoUtil.getAlias(Globals.myInfo),skyid, tpChat.tp_chat_getfriendsgid(), "");
							else{
								showToast(getString(R.string.addbuddy_exist));
							}
						}
						
					}
				}
			}
		});
		list = (ListView) this.findViewById(R.id.searchresult_list); // 联系人ListView
		list.setAdapter(adapter);// 将数据适配器与Activity进行绑定

	}
//	private void showToast(String str) {
//		Toast.makeText(SearchResultActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
	private List<Map<String, Object>> getData() {

		mData.clear();
		LinkedList<TPBuddyinfo> flist = tpChat.tp_chat_search_friends_data();
		if (flist != null) {
			for (TPBuddyinfo g : flist) {
				LogManager.LogShow("SearchResultActivity", "g.getName() " + g.getName());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("leftStr", BuddyInfoUtil.getAlias(g));
				if (g.getSignature().length() > 0)
					map.put("leftStr1", g.getSignature());
				map.put("leftImg", R.drawable.friends_default);
				map.put("rightImg", R.drawable.addfriends);
				map.put("obj", g);
				mData.add(map);
			}
		}
		return mData;

	}
}
